use std
use bio
use crypto
use "dispatch"

const sha256sum = {params
	
	var f, ofile
	var st, hash
	var buf : byte[1024]
	var b
	
	ofile=params[0]

    match bio.open(ofile, bio.Rd)
    | `std.Ok bio:        f = bio
    | `std.Err e:    std.fatal("Unable to open data file: {}\n", e)
    ;;
	
    crypto.sha256init(&st)
    
    while true
        b= r(f, buf[:])
        match b
        | "":    break;
        | _:       
			//std.put("{}\n",b)
			crypto.sha256add(&st,b)
        ;;
    ;;
	
    hash = crypto.sha256fin(&st)

	// komple raw data yazıyoruz
	std.put("{r}",hash[:])
	
	/* hex integer yazmak için
	for h : hash
			std.put("{x}", h)
	;;
	*/
	
	std.put("\n")
}

const r = {f, buf
	match bio.read(f, buf)
	| `std.Ok b:
		-> b
	| `std.Err `bio.Eof:
		std.put("eof\n")
		-> ""
	| `std.Err e:
		std.put("err\n")
		-> ""
	;;
}

const __init__ = {
	dispatch.register("sha256sum", sha256sum)
}
